const 
	_ = require('lodash'),
	uuid = require('uuid/v1'),
	fs = require('fs');

module.exports = async function(self, cmd, ...args){
	switch(cmd){
		case 'add':
			if (args.length>0) {
				if (_.find(self.namespace.webshells, {id: args[0]}))
					console.error('already exists.');
				else {
					self.namespace.webshells.push({id: uuid(), uri: args[0], method: args[1], pwd: args[2], type: args[3]});
					fs.writeFileSync('./webshells.json', JSON.stringify(self.namespace.webshells, null, 4));
				}
			}
			break;
		case 'del':
			if (args.length>0) {
				_.remove(self.namespace.webshells, item => item.id === args[0]);
			}
			break;
		case 'clear':
			self.namespace.webshells = [];
			break;
		case 'load':
			self.namespace.webshells = JSON.parse(fs.readFileSync('./webshells.json').toString());
			break;
		case 'run':
			// 根据target清单批量运行指定webshell
			if (args.length<1) {
				console.log('usage: ws run a31b0d90-05da-11ea-bf04-530e34712d94');
				return;
			}
			let ws = _.find(self.namespace.webshells, {id: args[0]});
			if (ws) {
				for(let target of self.namespace.targets){
					await require('./run')(self, target, ws);
				}
			} else console.error('webshell not found.');
			break;
		default:
			console.log(self.namespace.webshells);
			break;
	}
}